package com.humanmonth.home.component.page.signature.remote
{
	import com.humanmonth.global.Config;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import mx.controls.Alert;
	import mx.graphics.codec.JPEGEncoder;
	import mx.managers.CursorManager;
	
	/**
	 * 图片的上传及下载
	 * @author 林子龙
	 * 
	 */
	public class Connector
	{
		private var file:FileReference;
		private var myId:String;
		public function Connector()
		{
		}
		
		/**
		 * 保存图片
		 */
		public function savePic(myData:BitmapData,fun:Function):void{
		    CursorManager.setBusyCursor();
			var url:String=Config.picLink+"rea/pic.do?action=savePic&timestamp="+new Date().getTime();
 			var request:URLRequest = new URLRequest(url); 			
 			request.method=URLRequestMethod.POST;
		 	request.contentType = "application/octet-stream"; 
 			request.data=new JPEGEncoder(80).encode(myData);
            var loader:URLLoader = new URLLoader();                  
            loader.load(request) ;        
            loader.addEventListener(Event.COMPLETE, fun) ; 	
            loader.addEventListener(Event.COMPLETE,initMyId);
            Alert.show("正在上传图片，等待数秒后，即可下载图片");		
		}
		
		private function initMyId(event:Event):void{
		    CursorManager.removeBusyCursor();
			var loader:URLLoader=URLLoader(event.target);
			this.myId=loader.data;
			Alert.show("上传图片成功，现在可以点击‘下载图片’按钮，保存图片到本地。");
			
		}
		
		/**
		 * 下载图片 
		 */
		public function downloadFile(event:Event):void{
			var url2:String=Config.picLink+"rea/pic.do?action=queryPicById&pid="+myId+"&timestamp="+new Date().getTime();
			var req:URLRequest=new URLRequest(url2);
			file=new FileReference();
			file.download(req,"humanmonth.jpg");
		}
	}
}